//
//  Character.m
//  DefenseSummoner
//
//  Created by vupham on 5/23/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "GameCharacter.h"
#import "GameLayer.h"

@implementation GameCharacter

@synthesize HP, direction,inBattle, damage;

-(id)initCharacterWith:(GameLayer *)_gameLayer{
    self=[super initGameObjectWith:_gameLayer];
    speed=0;
    armor=0;
    HP=0;
    damage=0;
    attackRange=0;
    die = NO;
    
    bloodSprite = [[CCSprite alloc]initWithSpriteFrameName:@"blood_c_01.png"];    
    return self;
}

-(void)update:(ccTime)dt{
    //NSLog(@"Update GameObject");
}

-(void)loadAnimationBasedOnDirection:(int)_direction {
    if(_direction==DIRECTION_UP)
        [self animationMoveUp];
    else if(_direction==DIRECTION_LEFT)
        [self animationMoveLeft];
    else if(_direction==DIRECTION_DOWN)
        [self animationMoveDown];
    else
        [self animationMoveRight];
}

-(void)animationMoveLeft {
    spriteBody.flipX=NO;
}

-(void)animationMoveRight {
    [self animationMoveLeft];
    spriteBody.flipX=YES;
}

-(void)animationMoveUp {
    spriteBody.flipX=NO;
}

-(void)animationMoveDown {
    spriteBody.flipX=NO;
}

-(void)animationBattle {
    inBattle=YES;
    if (direction==DIRECTION_RIGHT)
        spriteBody.flipX=YES;
}

-(void)animationHit {
    [bloodSprite stopAllActions];
    [bloodSprite removeFromParentAndCleanup:YES];
    [spriteBody addChild:bloodSprite];
    
    //NSLog(@"blood retain count = %d", bloodSprite.retainCount);
    
    bloodSprite.visible = YES;
    bloodSprite.anchorPoint = ccp(0.5, 0);
    bloodSprite.position = ccp(spriteBody.contentSize.width/2, 0);
    
    //float scaleRate = spriteBody.contentSize.width / bloodSprite.contentSize.width;
    //bloodSprite.scale = scaleRate;
    
    CCAnimation *animtion = [self loadAnimationFromCacheByName:@"blood_c" withCount:6 delay:0.075f];
    [bloodSprite runAction:[CCSequence actions:[CCAnimate actionWithAnimation:animtion restoreOriginalFrame:NO], [CCHide action], nil]];
}

-(void)animationDie {
    //[self updateBloodMeter];
    die=YES;
    if (direction == DIRECTION_RIGHT)
        spriteBody.flipX = YES;
    [handler.listGameObject removeObject:self];
}

-(void)dieCallBack {
    [spriteBody removeFromParentAndCleanup:YES];
    [handler removeGameObject:self];
}

-(void)drawBloodMeterWithLength:(float)_length width:(float)_width position:(CGPoint)_position {
    bloodMeterRate = _length / HP;
    
    bloodMeterBackground = [[CCSprite alloc]initWithFile:@"Images/blank.png"];
    bloodMeterBackground.color = ccc3(0, 168, 0);
    bloodMeterBackground.textureRect = CGRectMake(0, 0, _length, _width);
    bloodMeterBackground.position = _position;
    [spriteBody addChild:bloodMeterBackground];
    
    bloodMeterRed = [[CCSprite alloc]initWithFile:@"Images/blank.png"];
    bloodMeterRed.color = ccc3(240, 38, 39);
    bloodMeterRed.textureRect = CGRectMake(0, 0, _length, _width);
    bloodMeterRed.anchorPoint = ccp(0, 0.5f);
    bloodMeterRed.position = ccp(_position.x - _length/2, _position.y);
    [spriteBody addChild:bloodMeterRed];
}

-(void)updateBloodMeter {
    float length = bloodMeterRate * HP;
    if(length < 0)
        length = 0;
    bloodMeterRed.textureRect = CGRectMake(0, 0, length, 3);
}

-(void)dealloc {
    [bloodSprite stopAllActions];
    [bloodSprite removeAllChildrenWithCleanup:YES];
    [bloodSprite removeFromParentAndCleanup:YES];
    [bloodSprite release];
    
    [bloodMeterRed stopAllActions];
    [bloodMeterRed removeFromParentAndCleanup:YES];
    [bloodMeterRed release];
    
    [bloodMeterBackground stopAllActions];
    [bloodMeterBackground removeFromParentAndCleanup:YES];
    [bloodMeterBackground release];
    
    [super dealloc];
}

@end
